#pragma comment(linker,"/STACK:100000000")
#include "testlib.h"
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
	registerValidation();
 	int n, i, j, q, r, c, k, A;
	string s;
	n=inf.readInt(1, 1000, "N"); inf.readSpace();
	q=inf.readInt(0, 5000000, "Q"); inf.readEoln();
	for(i=0; i<n; i++)
	{
		s=inf.readWord(); inf.readEoln();
		ensuref(s.size()==2*i+1, "there must be exactly %d digits", 2*i+1);
		for(j=0; j<2*i+1; j++)
			ensuref(s[j]>='0' && s[j]<='9', "only gigits 0-9 are allowed");
	}
	for(A=1; q--; A=((long long)A*1234567+7654321)%1000000007)
	{
		r=A%n+1;
		c=A%(2*r-1)+1;
		k=A%(n-r+1)+1;
		ensuref(r>=1 && r<=n, "triangle is outside");
		ensuref(c>=1 && c<=2*r-1, "triangle is outside");
		ensuref(r+k-1<=n, "triangle is outside");
	}
	inf.readEof();
	return 0;
}